<?php

namespace common\models\messages;

use Yii;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "{{%message}}".
 *
 * @property int $id 自增ID
 * @property int $item_id 项目ID
 * @property string $class 项目类型
 * @property string $title 标题
 * @property string $content 内容
 * @property string $user_ids 多个用户使用,连接
 * @property string $receive_port 接收端
 * @property int $status 状态(0等待/1完成/2停止)
 * @property string $details_link 详情链接
 * @property int $push_at 推动时间
 * @property int $created_by 创建者ID
 * @property int $updated_by 更改者ID
 * @property int $created_at 添加时间
 * @property int $updated_at 更新时间
 * @property int $script_lock 任务锁(0等待/1完成/2进行中)
 * @property string $file_path 上传文件路径
 * @property string $extra 额外信息json
 */
class Message extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%message}}';
    }

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
            ],
        ];
    }
    /**
     * @return \yii\db\Connection the database connection used by this AR class.
     */
//    public static function getDb()
//    {
//        return Yii::$app->get('db');
//    }

    /**
     * 接收端列表 (10^)
     * 1 代表 1
     * 10 代表 10
     * 11 代表 1,10
     * ...
     * @var array
     */
    public $receivePort = [
        '1' => '公众号',
        '10' => 'APP',
    ];

    /**
     * 根据接收端参数获取合集
     * @param int $port
     * @param bool $message
     * @return array
     */
    public function setPort($port = 0, $message = false)
    {
        $data = [];
        $num = strrev(strval($port));
        for ($i = 0; $i < strlen($num); $i++) {
            if ($num[$i]) {
                if ($message) {
                    $data[] = $this->receivePort[pow(10, $i)];
                } else {
                    $data[] = pow(10, $i);

                }
            }
        }
        return $data;
    }

    public static function createMessage($user, $title, $content, $param = [])
    {
        $model = new Message();
        $model->user_ids = implode(',', $user);
        $model->content = base64_encode($content);
        $model->title = $title;
        $model->created_by = 1;
        $model->receive_port = 10;
        $model->push_at = time();

        if (!empty($param['details_link'])) {
            $model->details_link = $param['details_link'];
        }
        if (!empty($param['item_id'])) {
            $model->item_id = $param['item_id'];
        }
        if (empty($param['class'])) {
            $model->class = strval(Message::className());
        }else{
            $model->class = $param['class'];
        }
        if(!empty($param["extra"]))
        {
            $model->extra   = json_encode($param["extra"]);
        }
        if ($model->save()) {
            if ($model->class == strval(Message::className())) {
                $model::updateAll(['item_id' => $model->id], ['id' => $model->id]);
            }
        }

        return $model;

    }
}
